package org.holynite.fieldmanage.repositories.impl

import org.holynite.fieldmanage.entities.ChargeScheme
import org.holynite.fieldmanage.persistence.ChargeSchemeEAO
import org.holynite.fieldmanage.repositories.BaseEAO

import javax.ejb.Stateless

/**
 * Created by midnite on 15/1/25.
 */
@Stateless(name = "chargeSchemeEao")
class ChargeSchemeEAOImpl extends BaseEAO implements ChargeSchemeEAO {
	@Override
	List<ChargeScheme> listAllScheme() {
		def query = em.createQuery("select cs from ChargeScheme as cs")
		query.resultList
	}

	@Override
	List<ChargeScheme> searchScheme(String keyword) {
		def query
		if (keyword.length() > 0) {
			query = em.createQuery("select cs from ChargeScheme as cs where cs.name like :keyword")
			query.setParameter("keyword", "%$keyword%".toString())
		} else {
			query = em.createQuery("select cs from ChargeScheme as cs")
		}
		query.resultList
	}

	@Override
	ChargeScheme findScheme(String schemeId) {
		def query = em.createQuery("select cs from ChargeScheme as cs where cs.schemeId=:schemeId")
		query.setParameter("schemeid", schemeId)
		query.singleResult as ChargeScheme
	}
}
